package com.cloudkinto.service.board.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.ContainerTemplateDao;
import com.cloudkinto.dao.FbaStorageDao;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.board.ChBoardService;
import com.cloudkinto.service.headercontainer.HeaderContainerService;
import com.cloudkinto.service.headercontainer.vo.HeaderContainerPageRes;
import com.cloudkinto.service.storage.StorageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author zhangyy
 * @date 2020/10/29 14:24
 * @description:
 */
@Service
public class ChBoardServiceImpl implements ChBoardService {
    @Autowired
    private HeaderContainerService headerContainerService;
    @Autowired
    private StorageService storageService;
    @Autowired
    private FbaStorageDao fbaStorageDao;
    @Autowired
    private ContainerTemplateDao containerTemplateDao;

    @Override
    public SingleResult getInfo() {
        Map map = new HashMap();
        map.put("storageList", storageService.getStorageList());

        List<Map> statusList = (List<Map>) headerContainerService.updateStatusInit().getData();
        map.put("statusList", statusList);

        return SingleResult.success(map);
    }

    @Override
    public SingleResult getInfoByDate(String startDate, String endDate, String storageId) {
        List<Map> result = new ArrayList<>();


        List<Map> statusList = (List<Map>) headerContainerService.updateStatusInit().getData();
        for (Map map : statusList) {
            LambdaQueryWrapper<HeaderContainerDo> wrapper = new LambdaQueryWrapper<>();
            wrapper.ge(HeaderContainerDo::getCreateTime, startDate)
                    .le(HeaderContainerDo::getCreateTime, endDate)
                    .eq(HeaderContainerDo::getStartStorageId, storageId);
            wrapper.eq(HeaderContainerDo::getStatus, map.get("id"));
            List<HeaderContainerDo> headerContainerDos = headerContainerService.getBaseMapper().selectList(wrapper);
            List<Map> mapList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(headerContainerDos)) {
                for (HeaderContainerDo headerContainerDo : headerContainerDos) {
                    Map map1 = new HashMap();
                    map1.put("id", headerContainerDo.getId());
                    map1.put("value", headerContainerDo.getContainerNumber());
                    mapList.add(map1);
                }
            }
            map.put("list", mapList);
            result.add(map);
        }
        return SingleResult.success(result);
    }

    @Override
    public SingleResult detail(Long id) {
        HeaderContainerDo headerContainerDo = headerContainerService.getBaseMapper().selectById(id);
        HeaderContainerPageRes res = new HeaderContainerPageRes();
        BeanUtils.copyProperties(headerContainerDo, res);
        StorageDo storageDo = storageService.getBaseMapper().selectById(headerContainerDo.getStartStorageId());
        if (storageDo != null) {
            res.setStorage(storageDo.getStorageName());
        }
        FbaStorageDo fbaStorageDo = fbaStorageDao.selectById(headerContainerDo.getEndStorageId());
        if (fbaStorageDo != null) {
            res.setFbaStorage(fbaStorageDo.getFbaCode());
        }
        ContainerTemplateDo containerTemplateDo = containerTemplateDao.selectById(headerContainerDo.getContainerId());
        if (containerTemplateDo != null) {
            res.setContainer(containerTemplateDo.getContainerCode());
        }
        res.setStatus(headerContainerDo.getStatus() == 1 ? "未完成" : "已完成");
        return SingleResult.success(res);
    }

}
